<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <!-- 主表单区域 -->
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="24" >
            <a-form-item label="类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['type', validatorRules.type]" :trigger-change="true" dictCode="policy_type" placeholder="请选择类型"/>
            </a-form-item>
          </a-col>
          <a-col :span="24" >
            <a-form-item label="子类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['subclass']" :trigger-change="true" dictCode="policy_subclass" placeholder="请选择子类型"/>
            </a-form-item>
          </a-col>
          <a-col :span="24" >
            <a-form-item label="标题" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['title', validatorRules.title]" placeholder="请输入标题"></a-input>
            </a-form-item>
          </a-col>
          <!--<a-col :span="24" >-->
          <!--  <a-form-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol">-->
          <!--    <j-dict-select-tag type="list" v-decorator="['status', validatorRules.status]" :trigger-change="true" dictCode="status" placeholder="请选择状态"/>-->
          <!--  </a-form-item>-->
          <!--</a-col>-->
          <a-col :span="24" >
            <a-form-item label="排序" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['top', validatorRules.top]" placeholder="请输入排序"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24" >
            <a-form-item label="简介" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['synopsis', validatorRules.synopsis]" placeholder="请输入简介"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24" >
            <a-form-item label="点击获取坐标" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a href="https://lbs.qq.com/tool/getpoint/get-point.html" target="_blank">https://lbs.qq.com/tool/getpoint/get-point.html</a>
            </a-form-item>

          </a-col>

          <a-col :span="24" >
            <a-form-item label="活动地点经度" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['longitude', validatorRules.longitude]" placeholder="如123.433399"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24" >
            <a-form-item label="活动地点纬度" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['latitude', validatorRules.latitude]" placeholder="如41.800932"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24" >
            <a-form-item label="预览图" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-image-upload isMultiple number="1" v-decorator="['previewImg']"></j-image-upload>
            </a-form-item>
          </a-col>
          <a-col :span="24" >
            <a-form-item label="内容" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-editor v-decorator="['content',{trigger:'input'}]"/>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
      <!-- 子表单区域 -->
    <a-tabs v-model="activeKey" @change="handleChangeTabs">
      <a-tab-pane tab="活动报名字段设置" :key="refKeys[0]" :forceRender="true">
        <j-editable-table
          :ref="refKeys[0]"
          :loading="lhygPolicyBmKeyTable.loading"
          :columns="lhygPolicyBmKeyTable.columns"
          :dataSource="lhygPolicyBmKeyTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="true"
          :actionButton="true"/>
<!--          <template v-slot:remark="props">-->
<!--            <a-select-->
<!--              mode="tags"-->
<!--              placeholder="Please select"-->
<!--              style="width: 200px"-->
<!--              :default-value="props.value"-->
<!--              @change="props.handleChange">-->
<!--              <a-select-option v-for="i in 25" :key="(i + 9).toString(36) + i">-->
<!--                {{ (i + 9).toString(36) + i }}-->
<!--              </a-select-option>-->
<!--            </a-select>-->
<!--          </template>-->
<!--        </j-editable-table>-->
      </a-tab-pane>
    </a-tabs>
  </a-spin>
</template>

<script>

  import pick from 'lodash.pick'
  import { getAction } from '@/api/manage'
  import { FormTypes,getRefPromise } from '@/utils/JEditableTableUtil'
  import { JEditableTableMixin } from '@/mixins/JEditableTableMixin'
  import { validateDuplicateValue } from '@/utils/util'
  import JFormContainer from '@/components/jeecg/JFormContainer'
  import JImageUpload from '@/components/jeecg/JImageUpload'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"
  import JEditor from '@/components/jeecg/JEditor'
  import JTagsSelect from '@/components/jeecg/j-tags-select.vue'

  export default {
    name: 'LhygPolicyForm',
    mixins: [JEditableTableMixin],
    components: {
      JFormContainer,
      JImageUpload,
      JDictSelectTag,
      JEditor,
      JTagsSelect // 注册新组件
    },
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        labelCol2: {
          xs: { span: 24 },
          sm: { span: 3 },
        },
        wrapperCol2: {
          xs: { span: 24 },
          sm: { span: 20 },
        },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
          type: {
            rules: [
              { required: true, message: '请输入类型!'},
            ]
          },
          title: {
            rules: [
              { required: true, message: '请输入标题!'},
              { pattern: /^.{1,50}$/, message: '请输入1到50位任意字符!'},
            ]
          },
          status: {
            rules: [
              { required: true, message: '请输入状态!'},
            ]
          },
          top: {
            rules: [
              { required: true, message: '请输入排序!'},
              { pattern: /^-?\d+$/, message: '请输入整数!'},
            ]
          },
          synopsis: {
            rules: [
              { required: true, message: '请输入简介!'},
              { pattern: /^.{1,100}$/, message: '请输入1到100位任意字符!'},
            ]
          },
          longitude: {
            rules: [
              { required: false},
              { pattern: /^[1-9]\d{1,2}\.\d{6}$/, message: '请输入正确的经度,如123.424289'},
            ]
          },
          latitude: {
            rules: [
              { required: false},
              { pattern: /^[1-9]\d{1,2}\.\d{6}$/, message: '请输入正确的纬度,如41.805165'},
            ]
          },
          content: {
            rules: [
              { required: true, message: '请输入内容!'},
            ]
          },
        },
        refKeys: ['lhygPolicyBmKey', ],
        tableKeys:['lhygPolicyBmKey', ],
        activeKey: 'lhygPolicyBmKey',
        // 活动报名字段设置
        lhygPolicyBmKeyTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '字段编号',
              key: 'fieldcode',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
              validateRules: [{ required: true, message: '${title}不能为空' },{ pattern: /^[a-z]{1,32}$/, message: '请输入32位以内小写字母,建议名称全拼活缩写!'},],
            },
            {
              title: '字段名称',
              key: 'fieldname',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
              validateRules: [{ required: true, message: '${title}不能为空' }, { pattern: /^.{1,32}$/, message: '请输入32位以内字符!'},],
            },
            {
              title: '字段是否必填',
              key: 'fieldisnull',
              type: FormTypes.select,
              dictCode:"yn",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '1',
            },
            {
              title: '字段类型',
              key: 'fieldtype',
              type: FormTypes.select,
              dictCode:"key_type",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '1',
            },
            {
              title: '字段最大长度',
              key: 'fieldmaxlength',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '10',
              validateRules: [{ pattern: "z", message: "${title}格式不正确,必须是数字" }],
            },
            {
              title: '下拉框备注',
              key: 'optionsRemark',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            // {
            //   title: '下拉框备注',
            //   key: 'optionsRemark',
            //   type: FormTypes.slot,
            //   slot: 'remark',
            //   width:"200px",
            //   placeholder: '请输入${title}',
            //   defaultValue: [],
            // },
          ]
        },
        url: {
          add: "/policy/lhygPolicy/add",
          edit: "/policy/lhygPolicy/edit",
          queryById: "/policy/lhygPolicy/queryById",
          updateStatus: "/policy/lhygPolicy/updateStatus",
          lhygPolicyBmKey: {
            list: '/policy/lhygPolicy/queryLhygPolicyBmKeyByMainId'
          },
        }
      }
    },
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: ()=>{},
        required: false
      },
      //表单模式：false流程表单 true普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    computed: {
      formDisabled(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return true
          }
        }
        return false
      }
    },
    created () {
      //如果是流程中表单，则需要加载流程表单data
      this.showFlowData();
    },
    methods: {
      // changeTags(e, item) {
      //   console.log("eeee", e)
      //   console.log("itemeeee", item)
      //   console.log("lhygPolicyBmKeyTable.dataSource", this.lhygPolicyBmKeyTable.dataSource)
      //   this.$nextTick(()=>{
      //     item.optionsRemark = e
      //   })
      //   console.log('refKeys[0]', this.$refs[this.refKeys[0]])
      // },
      addBefore(){
        this.form.resetFields()
        this.lhygPolicyBmKeyTable.dataSource=[]
      },
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        let fieldval = pick(this.model,'type','subclass','title','status','top','synopsis','longitude','latitude','previewImg','content')
        this.$nextTick(() => {
          this.form.setFieldsValue(fieldval)
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.lhygPolicyBmKey.list, params, this.lhygPolicyBmKeyTable)
        }
      },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)
        return {
          ...main, // 展开
          lhygPolicyBmKeyList: allValues.tablesValue[0].values,
        }
      },
      //渲染流程表单数据
      showFlowData(){
        if(this.formBpm === true){
          let params = {id:this.formData.dataId};
          getAction(this.url.queryById,params).then((res)=>{
            if(res.success){
              this.edit (res.result);
            }
          })
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },
     popupCallback(row){
       this.form.setFieldsValue(pick(row,'type','subclass','title','status','top','synopsis','longitude','latitude','previewImg','content'))
     },

    }
  }
</script>

<style scoped>
</style>